<?php
/**
 * Created by JetBrains PhpStorm.
 * User: MeRo
 * Date: 18.09.13
 * Time: 22:24
 * To change this template use File | Settings | File Templates.
 */

require_once 'templates/train/lib.php';
class getTemplateRail {

    protected $_echo = '';

    protected $_contao = NULL;

    protected $_elements = array();

    public function __construct($contao) {
        $this->_contao = $contao;

        $this
            ->_getElements()
            ->_generateHTML()
            ->_render()
        ;
        die();
    }

    protected function _addElement($element) {
        $this->_echo .= '
        ' . $element;

        return $this;
    }

    protected function _getElements() {
        $result = $this->_contao->Database->prepare('
            SELECT
                t.id,
                t.template_name,
                t.template_picture_path,
                t.template_route
            FROM
                tr_template t
            WHERE t.template_disable IS NULL
            ORDER BY t.id
        ')->execute();

        $this->_elements = $result->fetchAllAssoc();
        return $this;
    }

    protected function _generateHTML() {
        $count = count($this->_elements);
        $i = 0;
        foreach ($this->_elements as $element) {
            $last = $count == ($i++ + 1) ? 'last' : '';
            $this->_addElement('<div class="railTemplate brickTemplate pointer ' . $last . '">
                <img src="files/train/img/template/' . $element['template_picture_path'] . '.png" alt="' . $element['template_name'] . '">
                <div class="data invisible">' . $element['template_route'] . '</div>
            </div>');
        }

        return $this;
    }

    protected function _render() {
        echo json_encode($this->_echo);

        return $this;
    }
}

$this->import('Database');
new getTemplateRail($this);
